<template>
  <div class="login">
    <el-card class="box-card login-con">
      <div class="clearfix">
        <div class="title-box">
          <img src="../../assets/img/logo.png" alt="" class="img-logo" />
          <img src="../../assets/img/cn.png" alt="" class="img-cn" />
        </div>
        <!-- 登录表单 -->
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          ><div>{{ $t("lang.account") }}:</div>
          <el-form-item prop="account">
            <el-input v-model="ruleForm.account"></el-input>
          </el-form-item>
          <div>{{ $t("lang.password") }}:</div>
          <el-form-item prop="password">
            <el-input type="password" v-model="ruleForm.password"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')"
              >立即创建</el-button
            >
          </el-form-item>
          <el-form-item>
            <el-select
              v-model="lang"
              :placeholder="$t('lang.lang')"
              @change="changeLang"
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "loginView",
  data() {
    return {
      ruleForm: {
        account: "",
        password: "",
      },
      rules: {
        account: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            max: 12,
            message: "长度在 3 到 12 个字符",
            trigger: "blur",
          },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
      //语言集下拉框选项
      options: [
        { value: "zh", label: this.$t("lang.cs") },
        { value: "en", label: this.$t("lang.is") },
      ],
      lang: "", //语言集
    };
  },
  methods: {
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.$store.dispatch("LoginAction", this.ruleForm);
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    changeLang() {
      this.$store.commit("SET_LANG", this.lang);
      this. $router.go(0);
    },
  },
};
</script>

<style lang="scss" scoped>
.login {
  background: #ebeef2;
  height: 100vh;
  padding-top: 140px;
  .login-con {
    width: 380px;
    margin: auto;
    position: relative;
    text-align: center;
    .title-box {
      height: 50px;
      line-height: 50px;
      .img-cn {
        position: absolute;
        top: 0;
        right: 0;
        width: 50px;
      }
      .img-logo {
        width: 120px;
      }
    }
  }
}
</style>